﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TomShane.Neoforce.Controls;

namespace Engine2D.Windows
{
    class MainWindow : Window
    {
        private Window mSettingsWindow;

        private Engine2D Engine { get { return (Engine2D)Manager.Game; } }

        public MainWindow(Manager manager)
            : base(manager)
        {
            AutoScroll = false;
            Resize += new ResizeEventHandler(MainWindow_Resize);
        }

        void MainWindow_Resize(object sender, ResizeEventArgs e)
        {
            Control libraryPanel = GetControl("LibraryPanel");
            libraryPanel.Height = ClientHeight;
        }

        public override void Init()
        {
            mSettingsWindow = (Window)Layout.Load(Manager, "SettingsWindow");
            mSettingsWindow.Closed += new WindowClosedEventHandler(SettingsWindow_Closed);
            mSettingsWindow.Init();

            LibraryPanel panel = new LibraryPanel(Manager);
            panel.Name = "LibraryPanel";
            panel.Left = 0;
            panel.Top = 0;
            panel.Height = ClientHeight;
            panel.Width = 150;
            panel.Init();
            Add(panel);

            ContextMenu cMenu = new ContextMenu(Manager);
            MenuItem item1 = new MenuItem();
            item1.Text = "Insert";
            cMenu.Items.Add(item1);
            ContextMenu = cMenu;

            base.Init();
        }

        private void SettingsWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            Remove(mSettingsWindow);
        }

        public void ToggleSettings()
        {
            if (mSettingsWindow.Parent == null)
            {
                Add(mSettingsWindow);
                mSettingsWindow.Show();
            }
            else
            {
                mSettingsWindow.Hide();
                Remove(mSettingsWindow);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            MouseEventArgs ne = new MouseEventArgs(e);
            ne.Position = new Point(e.Position.X - ClientMargins.Left, e.Position.Y - ClientMargins.Top);
            Engine.Scene.OnMouseDown(ne);

            Focused = true;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            MouseEventArgs ne = new MouseEventArgs(e);
            ne.Position = new Point(e.Position.X - ClientMargins.Left, e.Position.Y - ClientMargins.Top);
            Engine.Scene.OnMouseMove(ne);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            MouseEventArgs ne = new MouseEventArgs(e);
            ne.Position = new Point(e.Position.X - ClientMargins.Left, e.Position.Y - ClientMargins.Top);
            Engine.Scene.OnMouseUp(ne);
        }

        protected override void OnMouseScroll(MouseEventArgs e)
        {
            base.OnMouseScroll(e);

            MouseEventArgs ne = new MouseEventArgs(e);
            ne.Position = new Point(e.Position.X - ClientMargins.Left, e.Position.Y - ClientMargins.Top);
            Engine.Scene.OnMouseScroll(ne);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            Engine.Scene.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);

            Engine.Scene.OnKeyUp(e);
        }
    }
}
